<?php
/**
* 
*/
class RevenueModel extends MY_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$this->table = 'nmp_user_card_log';
	}
	

	//统计
	public function income_count()
	{
		//总收入
		$query = $this->db->query("SELECT SUM(price) as total_num FROM ".$this->table);
		$res = $query->row_array();
		$data['total_num'] = $res['total_num'];

		//本月收入
		$start = date('Y-m-01 00:00:00');
		$end = date('Y-m-d H:i:s');
		$query = $this->db->query("SELECT SUM(price) as month_total FROM ".$this->table." where addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end')");
		$res = $query->row_array();
		$data['month_total'] = $res['month_total'] ? $res['month_total'] : '0.00';

		//上月收入
		$firstday = date('Y-m-01 00:00:00',strtotime('-1 month'));
  		$lastday = date("Y-m-d H:i:s", strtotime(date('Y-m-01 00:00:00'))-1);
		
  		$query = $this->db->query("SELECT SUM(price) as lastm_total FROM ".$this->table." where addtime >= unix_timestamp('$firstday') AND addtime <= unix_timestamp('$lastday')");
		$res = $query->row_array();
		$data['lastm_total'] = $res['lastm_total'] ? $res['lastm_total'] : '0.00';
		//同比
		$data['surplus'] = @round((intval($data['month_total']) - intval($data['lastm_total'])) / intval($data['lastm_total'])*100)."％";

		//本周收入
		$query = $this->db->query("SELECT SUM(price) as week_total FROM ".$this->table." WHERE YEARWEEK(FROM_UNIXTIME(addtime, '%Y-%m-%d')) = YEARWEEK(now())");
		$res = $query->row_array();
		$data['week_total'] = $res['week_total'] ? $res['week_total'] : '0.00';
		//上周
		$query = $this->db->query("SELECT SUM(price) as lastwk_total FROM ".$this->table." WHERE YEARWEEK(FROM_UNIXTIME(addtime,'%Y-%m-%d')) = YEARWEEK(now())-1;");
		$res = $query->row_array();
		$data['lastwk_total'] = $res['lastwk_total'] ? $res['lastwk_total'] : '0.00';

		$data['week_surplus'] = @round((intval($data['week_total']) - intval($data['lastwk_total'])) / intval($data['lastwk_total'])*100)."％";

		return $data;
	}



	//折线图数据
	public function income_data($dtnum = 90)
	{
		$date_arr = array();
		$appoint_arr = array();

		if (!is_numeric($dtnum)) 
		{
			//处理日期
			$d1 = substr($dtnum,0,10);
			$d2 = substr($dtnum,-10);
			$dlist = $this->getDateFromRange($d1,$d2);

			for ($i=0; $i < count($dlist); $i++) 
			{
				$date_arr[] = substr($dlist[$i],-5);

				$start = date('Y-m-d 00:00:00',strtotime($dlist[$i]));
				$end = date('Y-m-d H:i:s',strtotime('+1 day',strtotime($start))-1);
				
				$query = $this->db->query("SELECT SUM(price) as revenue_num FROM ".$this->table." WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end')");
				$res = $query->row_array();

				$appoint_arr[] = $res['revenue_num'] ? $res['revenue_num'] : 0;
			}
		}
		else
		{
			for ($i=$dtnum; $i > 0; $i--) 
			{
				$date_arr[] = date('m-d',strtotime('-'.$i.' day'));

				$start = date('Y-m-d 00:00:00',strtotime('-'.$i.' day'));
				$end = date('Y-m-d 00:00:00',strtotime('-'.($i-1).' day'));

				$query = $this->db->query("SELECT SUM(price) as revenue_num FROM ".$this->table." WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end')");
				$res = $query->row_array();

				$appoint_arr[] = $res['revenue_num'] ? $res['revenue_num'] : 0;
			}

		}

		$data['date_list'] = $date_arr;
		$data['revenue_num'] = $appoint_arr;

		return $data;

	}


	/**
	 * 获取指定日期段内每一天的日期
	 * @param  Date  $startdate 开始日期
	 * @param  Date  $enddate   结束日期
	 * @return Array
	 */
	function getDateFromRange($startdate, $enddate)
	{
	    $stimestamp = strtotime($startdate);
	    $etimestamp = strtotime($enddate);

	    // 计算日期段内有多少天
	    $days = ($etimestamp-$stimestamp)/86400+1;

	    // 保存每天日期
	    $date = array();
	    for($i=0; $i<$days; $i++){
	        $date[] = date('Y-m-d', $stimestamp+(86400*$i));
	    }

	    return $date;
	}


	//获取购卡记录
	public function user_card_log($sdate,$k,$page=1,$size=50)
	{
		$start = date('Y-m-01 00:00:00');
		$end = date('Y-m-d H:i:s');

		if (!empty($sdate)) {
			//处理日期
			$d1 = substr($sdate,0,10);
			$d2 = substr($sdate,-10);

			$start = date('Y-m-d H:i:s',strtotime($d1));
			$end = date('Y-m-d H:i:s', strtotime($d2)+86400);
		}

		$where = "lg.addtime >= unix_timestamp('$start') AND lg.addtime <= unix_timestamp('$end')";

		if (!empty($k)) {
			$where .= " AND (u.nickname LIKE '%".$k."%' OR u.mobile LIKE '%".$k."%')";
		}
		$sql  ="SELECT lg.*, sc.card_name, c.name as course_name, u.nickname FROM nmp_user_card_log as lg 
			LEFT JOIN nmp_user as u ON lg.user_id = u.id 
			JOIN nmp_course_card as sc ON lg.card_id = sc.id 
			JOIN nmp_course as c ON sc.course_id = c.id 
			WHERE ".$where." ORDER BY lg.addtime DESC LIMIT ".($page-1)*$size.",".$size;

		$query = $this->db->query($sql);
		$data = $query->result_array();

		$total = 0;
		foreach ($data as $key => $value) 
		{
			$data[$key]['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
			$total = $total + $value['price'];
		}

		$query = $this->db->query("select COUNT(*) as num from nmp_user_card_log as lg 
			LEFT JOIN nmp_user as u ON lg.user_id = u.id 
			JOIN nmp_course_card as sc ON lg.card_id = sc.id 
			JOIN nmp_course as c ON sc.course_id = c.id 
			WHERE ".$where);
		$row = $query->row_array();

		//总数
		$count = $row['num'];
		//总页数
		$pages = ceil($count/$size);
		
		return array('list'=>$data,'count'=>$count, 'pages'=>$pages, 'total'=>sprintf("%.2f",$total));
	}



}

?>